package com.vay.de.dang.online.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.util.Locale;

import com.vay.de.dang.online.entiy.AppConfigEntity;
import com.vay.de.dang.online.entiy.SignInEntity;
import com.vay.de.dang.online.web.JsonUtil;

public final class VnContext implements VnDefine {

    private static VnContext _instance;

    private Context mContext;
    private VnPfReader mPf;

    private String mToken;
    private String mPhone;
    private SignInEntity mLoginEntity;
    private AppConfigEntity mHomeConfig;

    public static String packageName;
    public static ApplicationInfo appInfo;
    public static String versionName;
    public static String versionCode;
    public static String language;
    public static String countryCode;

    private VnContext(Context context) {
        mPf = VnPfReader.instance();
        mContext = context;
        //mToken = mPf.readString(LocalToken);
        mPhone = mPf.readString(LocalPhone);

        // 包名
        packageName = context.getPackageName();
        try {
            appInfo = context.getPackageManager().getApplicationInfo(packageName,
                    PackageManager.GET_META_DATA);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Locale locale = context.getResources().getConfiguration().getLocales().get(0);
        language = locale.getLanguage();
        if (language.equalsIgnoreCase("zh")) {
            countryCode = "CN";
        } else {
            countryCode = "VNM";
        }

        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionCode = String.valueOf(packInfo.versionCode);
        } catch (Exception var2) {
            versionCode = "0";
        }

        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packInfo.versionName;
        } catch (Exception var2) {
            versionName = "1.0.0";
        }

        // load local user info
        String json = mPf.readString(LocalUser);
        if (!TextUtils.isEmpty(json)) {
            saveLogin(JsonUtil.toLoginEntity(json));
        }
    }

    public static void create(Context context) {
        _instance = new VnContext(context);
    }

    public static VnContext instance() {
        return _instance;
    }

    public Context getContext() {
        return mContext;
    }

    public void saveLogin(SignInEntity entity) {
        if (entity != null) {
            mLoginEntity = entity;
            mToken = entity.getToken();
            mPhone = entity.getPhone();
            mPf.putString(LocalToken, mToken);
            mPf.putString(LocalPhone, mPhone);
            mPf.putString(LocalUser, JsonUtil.objectToJson(entity));
        }
    }

    public void saveHomeConfig(AppConfigEntity entity) {
        mHomeConfig = entity;
    }

    public AppConfigEntity getAppConfig() {
        return mHomeConfig;
    }

    public String getPhone() {
        return mPhone;
    }

    public void logout() {
        mToken = null;
        mPhone = null;
        mPf.putString(LocalToken, "");
        mPf.putString(LocalPhone, "");
        mPf.putString(LocalUser, "");
        mLoginEntity = null;
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(mToken);
    }

    public String getToken() {
        return mToken;
    }

    public String getUserId() {
        return mLoginEntity != null ? mLoginEntity.getUserId() : "";
    }

    public String getLanguage() {
        if (!TextUtils.isEmpty(language)) return language;
        Locale locale = mContext.getResources().getConfiguration().getLocales().get(0);
        language = locale.getLanguage();
        if (language.equalsIgnoreCase("zh")) {
            countryCode = "CN";
        } else {
            countryCode = "VNM";
        }
        return language;
    }

    public String getCountryCode() {
        return "VNM"/*TextUtils.isEmpty(countryCode) ? "VNM" : countryCode*/;
    }
}
